<html>
    <head>
        <title>Calculator</title>
    </head>
<body>

<h1>Calculator</h1>
(04/07/2012 by Jessica Kain)
<br>

<p>

<!--
 I used the GET method to retrieve/execute the user input. Below, I created 
a submit button in html and NOT php. When the user
inputs an expression and presses the button, the php code handles the execution.
-->

<form method = "GET">
<input type="text" name="in_expr">
<input type="submit" value="Calculate">
</form>

</p>

<?php

// Retrieve the inputted expression and load into a variable.
$expr = $_GET["in_expr"];

if($expr)
{
    //$alpha = 0;
    $pattern = 0;
    // DID NOT USE THE FOLLOWING:
    //for ($i=0; $i<strlen($expr); $i++)
    //{
        // If there is a letter in the expression, output an error message.
    //    if (ctype_alpha($expr["$i"]))
    //    {
    //        static $alpha = 1;
    //        echo "<h2>Result</h2>";
    //        echo "Invalid input expression ".$expr;
    //        break;
    //    }
    //}
    
    // Find invalid patterns within an expression and output an error message.
    // Invalid Test cases:
    // 1. Check for '=','%','&','|','!' in input DID NOT USE!!!
    // 2. Check for more than one '+','-','/', '*', or '.' in input
    // 3. Check for '+','-','/','*', or '.' as the first character in input
    // NOTE: It is OKAY for the first character to be either '+' or '-'
    // AS LONG AS it is followed by a digit! The reason for this is because
    // these can act as signs and not operators.
    // 4. Check for '+','-','/', or '*' as the last character in input
    // NOTE: I do not need to check for '.' as the last character of input since
    // eval handles this without throwing a parse error.
    // 5. Check for non-digits that are not '+','-','/','*', or spaces
    //    in input
    // 6. Check for only spaces (aka no operators) between digits in input
    if (//preg_match('#=#', $expr) || preg_match('#%#', $expr) || 
        //preg_match('#&#', $expr) || preg_match('#\|#', $expr) ||
        //preg_match('#!#', $expr) || 
        preg_match("#\+\+#", $expr) ||
        preg_match("#\-\-#", $expr) || preg_match("#\/\/#", $expr) ||
        preg_match("#\*\*#", $expr) ||  preg_match("#\.\.#", $expr) ||
        (preg_match("#^\+#", $expr) && !preg_match("#^\+\d#", $expr)) ||
        (preg_match("#^\-#", $expr) && !preg_match("#^\-\d#", $expr)) ||
        preg_match("#^\*#", $expr) ||  preg_match("#^\/#", $expr) || 
        preg_match("#^\.#", $expr) ||
        preg_match("#\+$#", $expr) ||  preg_match("#\-$#", $expr) ||   
        preg_match("#\*$#", $expr) ||  preg_match("#\/$#", $expr) ||
        preg_match("#[^\d\+\-\*\/\s\.]#", $expr) ||  
        preg_match("#\d\s+\d#", $expr)  )
    {
        $pattern = 1;
        echo "<h2>Result</h2>";    
        echo "Invalid input expression ".$expr.".";
    }
    
    // Divide by 0
    if (preg_match('#\/0#', $expr) && !preg_match('#\/0\.\d*[1-9]#', $expr))
    {
        $pattern = 1;
        echo "<h2>Result</h2>";
        echo "Invalid input expression ".$expr.".";
        //echo $expr." = ";
    }
    
    // If no letters or invalid patterns are found, execute the expression by
    // using the php eval() function. Then, result is outputted.
    if($pattern != 1)
    {
        // DID NOT USE THIS WAY!!!! Implemented via preg_match (see above)
        // For division by 0, no exception should be outputted. To handle this,
        // I created the variables $index1 and $index2 and loaded them with the
        // second to last and last elements of the expression. If there is
        // division by 0, then output the inputted expression without an 
        // exception or evaluation. Else, execute the eval function.
        //$index1 = strlen($expr)-2;
        //$index2 = strlen($expr)-1;
        //if(($expr["$index1"] == "/") && ($expr["$index2"] == "0"))
        //{
        //    echo "<h2>Result</h2>";
        //    echo $expr." = ";
        //}
        //else    
        //{
            // As per the hint in the php tutorial slides posted on the class
            // website, I used the imbedded php eval function to execute the
            // expression inputted by the user. The function handles negative
            // numbers, fractions, decimal values...etc.
            eval("\$ans=$expr;");
            echo "<h2>Result</h2>";
            echo $expr." = ".$ans;
        //}
    }

}

?>

</body>
</html>




